package cn.com.xiaoshoujia.activities.activity;

import cn.com.xiaoshoujia.activities.dto.ActivityOperationAddDTO;
import cn.com.xiaoshoujia.activities.dto.ActivityOperationDTO;
import cn.com.xiaoshoujia.activities.dto.ActivityOperationQueryDTO;
import cn.com.xiaoshoujia.activities.entity.ActivityOperation;
import cn.com.xiaoshoujia.activities.mapper.ActivityOperationMapper;
import cn.com.xiaoshoujia.common.entity.City;
import cn.com.xiaoshoujia.common.mapper.CityMapper;
import cn.com.xiaoshoujia.company.entity.CompanyLogin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 * @author dulei
 * @Title: ActivityOperationActivity
 * @Description: 活动设置Activity
 * @Time 2017/7/27 14:39
 */
@Repository
public class ActivityOperationActivity {

    private Logger logger = LoggerFactory.getLogger(ActivityOperationActivity.class);

    @Autowired
    ActivityOperationMapper activityOperationMapper;

    @Autowired
    CityMapper cityMapper;

    /**
     * @Author: dulei
     * @Description:
     * @param: xcode 查询团购设置信息
     * @Date: 2017/7/28 11:47
     */
    public ActivityOperationDTO findOperation(String xcode) {
        logger.debug("获得团购设置信息");
        ActivityOperation activityOperation = activityOperationMapper.findByCompanyXcode(xcode);
        if (null == activityOperation)
            return null;
        ActivityOperationDTO activityOperationDTO = new ActivityOperationDTO();
        BeanUtils.copyProperties(activityOperation, activityOperationDTO);
        logger.debug("获得团购上级城市");
        City city = cityMapper.findByCityId(activityOperationDTO.getCityId());
        if (null != city) {
            activityOperationDTO.setProvinceId(city.getParentId());
        }
        return activityOperationDTO;
    }

    /**
     * @Author: dulei
     * @Description: 发布拼团回显信息
     * @param: * @param null
     * @Date: 2017/7/28 11:56
     */
    public ActivityOperationQueryDTO findTeamOperation(String xcode) {
        logger.debug("获得团购设置信息");
        ActivityOperation activityOperation = activityOperationMapper.findByCompanyXcode(xcode);
        if (null == activityOperation)
            return null;
        ActivityOperationQueryDTO operation = new ActivityOperationQueryDTO();
        BeanUtils.copyProperties(activityOperation, operation);
        logger.debug("获得团购上级城市");
        City city = cityMapper.findByCityId(operation.getCityId());
        if (null != city) {
            operation.setProvinceId(city.getParentId());
        }
        return operation;
    }

    public boolean updateOperation(ActivityOperationAddDTO operationAddDTO, CompanyLogin com) {
        ActivityOperation activityOperation = new ActivityOperation();
        BeanUtils.copyProperties(operationAddDTO, activityOperation);
        activityOperation.setFkCompanyXcode(com.getXcode());
        activityOperation.setMendTime(new Date());
        activityOperation.setMenderId(com.getID());
        activityOperation.setMender(com.getUsername());
        logger.debug("更新设置信息");
        if (activityOperationMapper.update(activityOperation) != 1) {
            throw new RuntimeException("更新失败");
        }
        return true;
    }

    public boolean saveOperation(ActivityOperationAddDTO operationAddDTO, CompanyLogin com) {
        ActivityOperation activityOperation = new ActivityOperation();
        BeanUtils.copyProperties(operationAddDTO, activityOperation);
        activityOperation.setFkCompanyXcode(com.getXcode());
        activityOperation.setCreateTime(new Date());
        activityOperation.setCreatorId(com.getID());
        activityOperation.setCreator(com.getUsername());
        logger.debug("保存设置信息");
        if (activityOperationMapper.save(activityOperation) != 1) {
            throw new RuntimeException("保存失败");
        }
        return true;
    }

    /**
     * @Author: dulei
     * @Description: 查找拼团活动设置是否存在
     * @param: xcode 企业唯一编码
     * @Date: 2017/7/28 14:09
     */
    public ActivityOperation findByCompanyXcode(String xcode) {
        return activityOperationMapper.findByCompanyXcode(xcode);
    }
}
